package com.zlm.IO.ByteStream.FileInputStream;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 13026
 */
public class FileInputStream_demo {
    /*
     * FileInputStream
     * 字节输入流
     *
     * void read(int b)                   一次读取一个字节
     * void read(byte[] b)                一次读取一个字节数组
     * */
    public static void main(String[] args) throws IOException {
        // 读取一个字节
        // 如果文件不存在，直接报错
        FileInputStream fos = new FileInputStream("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\FileInputStream\\FilesPackage\\a.txt");
        // 如果读不到会返回-1
        int read = fos.read();
        System.out.println(read + "=>" + (char) read);
        fos.close();

        // 循环读取
        FileInputStream fos2 = new FileInputStream("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\FileInputStream\\FilesPackage\\a.txt");
        int b;
        while ((b = fos2.read()) != -1) {
            System.out.println("循环读取：" + b + "=>" + (char) b);
        }
        fos2.close();

        // 一次读取多个数据
        // 返回值，本次读取多少字节数据
        FileInputStream fos3 = new FileInputStream("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\FileInputStream\\FilesPackage\\a.txt");
        byte[] bytes = new byte[1024];
        int length;
        while ((length=fos3.read(bytes))!=-1){
            System.out.println(length);
        }

        fos3.close();
    }
}

